package com.ly.web.controller.admin;

import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.ly.base.*;
import com.ly.entity.SupportAddress;
import com.ly.service.IUserService;
import com.ly.service.ServiceMultiResult;
import com.ly.service.ServiceResult;
import com.ly.service.house.IAddressService;
import com.ly.service.house.IHouseService;
import com.ly.service.house.IQiNiuService;
import com.ly.service.search.ISearchService;
import com.ly.web.dto.*;
import com.ly.web.form.DatatableSearch;
import com.ly.web.form.DatatableUserSearch;
import com.ly.web.form.HouseForm;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Pair;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * 管理员接口
 * @author ：LY
 * @date ：Created in 2020/11/8 17:35
 * @modified By：
 */
@Controller
public class AdminUserController {
    @Autowired
    private IQiNiuService iQiNiuService;

    @Autowired
    private IAddressService addressService;

    @Autowired
    private IHouseService iHouseService;

    @Autowired
    private IUserService userService;

    @Autowired
    private ISearchService searchService;

    @Autowired
    private Gson gson;
    @PutMapping("admin/user/operate/{id}/{operation}")
    @ResponseBody
    public ApiResponse operateHouse(@PathVariable(value = "id")Long id,
                                    @PathVariable(value = "operation") int operation){
        if (id<=0){
            return ApiResponse.ofStatus(ApiResponse.Status.NOT_VALID_PARAM);
        }
        ServiceResult result;
        switch (operation){
            case 0:
                result = this.userService.updateStatus(id, UserStatus.NOT_AUDITED.getValue());
                break;
            case 1:
                result = this.userService.updateStatus(id, UserStatus.PASSES.getValue());
                break;
            case 2:
                result = this.userService.updateStatus(id, UserStatus.DELETED.getValue());
                break;
            default:
                return ApiResponse.ofStatus(ApiResponse.Status.BAD_REQUEST);
        }
        if (result.isSuccess()){
            return ApiResponse.ofSuccess(null);
        }
        return ApiResponse.ofMessage(HttpStatus.BAD_REQUEST.value(),result.getMessage());
    }

    /**
     * 获取预约用户信息
     * @param userId
     * @return
     */
    @GetMapping("admin/user/{userId}")
    @ResponseBody
    public ApiResponse getUserInfo(@PathVariable(value = "userId")Long userId){
        if (userId  == null || userId <1){
            return ApiResponse.ofStatus(ApiResponse.Status.BAD_REQUEST);

        }
        ServiceResult<UserDTO> serviceResult = userService.findById(userId);
        if (!serviceResult.isSuccess()){
            return ApiResponse.ofStatus(ApiResponse.Status.NOT_FOUND);

        }else {
            return ApiResponse.ofSuccess(serviceResult.getResult());
        }
    }

    /**
     * 预约完成接口
     * @param houseId
     * @param userId
     * @return
     */
    @PostMapping("admin/finish/subscribe")
    @ResponseBody
    public ApiResponse finishSubscribe(@RequestParam(value = "house_id")Long houseId,@RequestParam(value = "user_id")Long userId){
        if (houseId<1||userId<1){
            return ApiResponse.ofStatus(ApiResponse.Status.BAD_REQUEST);
        }
        ServiceResult result = iHouseService.finishSubscribe(houseId,userId);
        if (result.isSuccess()){
            return ApiResponse.ofSuccess("");
        }else {
            return ApiResponse.ofMessage(ApiResponse.Status.BAD_REQUEST.getCode(),result.getMessage());
        }
    }

    /**
     * 获取待编辑用户的信息
     * @param id
     * @param model
     * @return
     */
    @GetMapping("admin/user/edit")
    public String userEditPage(@RequestParam(value = "id") Long id, Model model){
        if (id == null||id<1){
            return "404";
        }

        ServiceResult<UserDTO> completeOne = userService.findCompleteOne(id);

        if (!completeOne.isSuccess()){
            return "404";
        }
        UserDTO result = completeOne.getResult();

        model.addAttribute("user",result);
        return "admin/user-edit";
    }

    /**
     * 编辑接口
     * @param userDTO
     * @return
     */
    @PostMapping("admin/user/edit")
    @ResponseBody
    public ApiResponse saveUser(@Valid @ModelAttribute("form-user-deit") UserDTO userDTO){
        ServiceResult update = userService.update(userDTO);
        if (update.isSuccess()){
            //更新成功
            return ApiResponse.ofSuccess(null);
        }
        ApiResponse response = ApiResponse.ofStatus(ApiResponse.Status.BAD_REQUEST);
        response.setMessage(update.getMessage());

        return response;
    }


}
